/**
 * @Filename: TLinxAESCoder.java
 * @Author锛歝aiqf
 * @Date锛�016-4-12
 */
package com.common.util;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
/**
 * @Class: TLinxAESCoder.java
 * @Description: AES加解密类
 * @Author：caiqf
 * @Date：2016-4-12
 */
public class TLinxAESCoder {
	private static String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
	private static String KEY_ALGORITHM = "AES";

	public static String decrypt(String sSrc, String sKey) throws Exception {
		SecretKeySpec skeySpec = new SecretKeySpec(sKey.getBytes("ASCII"),
				KEY_ALGORITHM);
		Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
		cipher.init(2, skeySpec);
		byte[] encrypted1 = hex2byte(sSrc);
		byte[] original = cipher.doFinal(encrypted1);
		return new String(original,"UTF-8");
	}

	public static String encrypt(String sSrc, String sKey) throws Exception {
		SecretKeySpec skeySpec = new SecretKeySpec(sKey.getBytes("ASCII"),
				KEY_ALGORITHM);
		Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
		cipher.init(1, skeySpec);
		byte[] encrypted = cipher.doFinal(sSrc.getBytes("UTF-8"));
		return byte2hex(encrypted);
	}

	private static byte[] hex2byte(String strhex) {
		if (strhex == null)
			return null;

		int l = strhex.length();
		if (l % 2 == 1)
			return null;

		byte[] b = new byte[l / 2];
		for (int i = 0; i != l / 2; ++i)
			b[i] = (byte) Integer.parseInt(strhex.substring(i * 2, i * 2 + 2),
					16);

		return b;
	}

	private static String byte2hex(byte[] b) {
		String hs = "";
		String stmp = "";
		for (int n = 0; n < b.length; ++n) {
			stmp = Integer.toHexString(b[n] & 0xFF);
			if (stmp.length() == 1)
				hs = hs + "0" + stmp;
			else
				hs = hs + stmp;
		}

		return hs.toUpperCase();
	}

	public static void main(String[] args) throws Exception {
		/*String text="A13E2C877A2305FC4B587764BD2F6EF833C518349E87EE01056EE919E080985745EFB3D3168009A498A0F0575E973460A7F387AC980E3CDD898AA5E70983536933D4B0E1CA0FE26325C9DB750F4CF1567C3EBDD9E3F0534C222AB401C33996DB40D8F3F572720EBE41457C8AD1AA87147CFB1CDACD0D7C6AC97871572B21FE16DB671209E982F0D7712E5B06EF9DE7DB2A8608E86FF84CB416EDF70F98B9CA361279953CAA1214CD3017BBF752982457E231E43C6050D931E6F38CCED984726A67E1F2EE1F6F97B0704B7C436FB455401B869C93F878F362FAF45DD632287E0EFA42CD5A0C55341255F586A0E330342CF2806B729B78167865F14EDDB57538A5C65E8ABFB08297F8526F835538E306B001FB5EAF225807C53E62BB485DE9FF3F50FC563711E201695C71CF9ACB779233FAAD623F33493674A1E0C21000AB89D4F5164E463CE13DDB3CC6E8042F4B92666F5532E62FD4EA7F813990867E7C752D169244E2C3CE28C404CF86F42E12DB90A797C5469673C39488F1C0E07984E809F2806B729B78167865F14EDDB57538A5C65E8ABFB08297F8526F835538E306B001FB5EAF225807C53E62BB485DE9FF3F50FC563711E201695C71CF9ACB779233FAAD623F33493674A1E0C21000AB89D4F5164E463CE13DDB3CC6E8042F4B92666F5532E62FD4EA7F813990867E7C752D169244E2C3CE28C404CF86F42E12DB90A797C5469673C39488F1C0E07984E809045A1D8E676149DDE9B97E88B7CA16AF967EE457EB845F0D4A2F55B7EA7BD4E57631A79C40680AD1D8DE125E4EF15D2478C20590E5FACC147AEDCF97B2A5BFCBE525514688D9397BB496F1205CF5F46949AF60102DC75A59BECD61E8F5D3E11C1E663D3A1DCB03859570B17B1017C678F09F1F984A1CC6BB13BAAA0082B3DC9EC6AC1E37CD34F8202C4870792F8323AC9FF76E4CB8C56461166CB65A3B60CCDD0C5C6BEE504B37FBEC5280583FCD851159213249881416FD5785000F8BB0AED6D442128D02518FE84D108E31A94A83022B4BB606EE26F209FD29E3BA2D07AAF8B8DCAF778391489C6C6AB01B985935CD4CDC2CD554D5692F86B6CD3BB20BB6B623937F4328B2AB5D811F7B20D6A135873B7ADC797E1315D23350F6D703D306C6CDF1E6876FD4E1BED37A4B7E2385B306C1582E4E5BB0BEB74C4F6119429090872CB2CF71A60D2F74D8A0D0B6CFEE6783920450B8E204EF10D73DA5FE8D5EEB79F7F3C9B86C8A9FFB6B8B15FF0EFEBDE3D394339F536DF589AD94A0D66476A9C8F242D6768B767593B96050E2DF7A1857223B42F2DE5F6521187DEA7D25B8E9F8C4EB2C0B7B8D3FB77C387062E95804F5F242D6768B767593B96050E2DF7A1857361D15759F67C985726DF0C64E10BC1966FD8C41DDEAAFE6F6195FB947830441F813567819C6C39A294BCBF081D5D38FC18AD1AE35C67FC539177E4BCF2B5B8DEFB52A39AFDEA807E2F356A8C805EC18EA0F32F53D1A58E198D1107945D6E13C125EE9743FF5CA07A14045B54FE5EC2BDF55A1E03E487017E3E3DEBC93D209660DA01F1FFE9831003FB2035733AF7904FFF1827FB3E4E1B418F8A39B207A0B312118BE4E3CEE45F82E0DCCDB92C4169E6D1884741DA5714915762B30BDFC367600C5CABCFCB77CC8A6EC672F5B9DE8B15BE28F3D180CE37A2559ED48146C0FF84EDF61B91027937ECA1539726385B343C1866A4C8801BF0EA41476756BDA062EDEA6AF497FE11EB75B10F47DEF5351848E7A4B677B83B3B6C7952998487F1135F242D6768B767593B96050E2DF7A1857868549407206CA374D5C54C21399A2AB07F09778BA7CFF2A5347E059E01E7ABC3E6A7D9651964E711831579002EDF2519BFCF7AFB2B92245639A5317E5BEADAA9EA6FEF89D478BA06F152B30C9A3F63175CD89A8EFFB0FF8D68C6E9E2CE14294166EFF7B9316331F3F3843B3F97321570E318ED3FC9432C010A1CA5D07F2B9A91E571BC4957AAC3D1572FEDC48EE53549437ABF39EFEFC6229D5E7ECF0644C6E51B4B31519624F2C5EDC125F6E4055A7C699CAB5BC93BA5F7B8C7FDFB925A1B879C7AE2838D4C8074B2BD54D2C5D5AE960A50F8B34DF2F5DE64F79765BC3385A";
	    String ret=decrypt(text,"12345678901234567890123456789012");
		System.out.println(ret);*/

	/*	String key="6df8db3437834d149e44c357f9ccaaab";
		System.out.println(key.getBytes("ASCII").length);
		String secret=encrypt("23232323",key);
		//System.out.println(secret);
		//secret="A13E2C877A2305FC4B587764BD2F6EF833C518349E87EE01056EE919E080985745EFB3D3168009A498A0F0575E973460A7F387AC980E3CDD898AA5E70983536933D4B0E1CA0FE26325C9DB750F4CF1567C3EBDD9E3F0534C222AB401C33996DB40D8F3F572720EBE41457C8AD1AA87147CFB1CDACD0D7C6AC97871572B21FE16DB671209E982F0D7712E5B06EF9DE7DB2A8608E86FF84CB416EDF70F98B9CA361279953CAA1214CD3017BBF752982457E231E43C6050D931E6F38CCED984726A67E1F2EE1F6F97B0704B7C436FB455401B869C93F878F362FAF45DD632287E0EFA42CD5A0C55341255F586A0E330342CF2806B729B78167865F14EDDB57538A5C65E8ABFB08297F8526F835538E306B001FB5EAF225807C53E62BB485DE9FF3F50FC563711E201695C71CF9ACB779233FAAD623F33493674A1E0C21000AB89D4F5164E463CE13DDB3CC6E8042F4B92666F5532E62FD4EA7F813990867E7C752D169244E2C3CE28C404CF86F42E12DB90A797C5469673C39488F1C0E07984E809F2806B729B78167865F14EDDB57538A5C65E8ABFB08297F8526F835538E306B001FB5EAF225807C53E62BB485DE9FF3F50FC563711E201695C71CF9ACB779233FAAD623F33493674A1E0C21000AB89D4F5164E463CE13DDB3CC6E8042F4B92666F5532E62FD4EA7F813990867E7C752D169244E2C3CE28C404CF86F42E12DB90A797C5469673C39488F1C0E07984E809045A1D8E676149DDE9B97E88B7CA16AF967EE457EB845F0D4A2F55B7EA7BD4E57631A79C40680AD1D8DE125E4EF15D2478C20590E5FACC147AEDCF97B2A5BFCBE525514688D9397BB496F1205CF5F46949AF60102DC75A59BECD61E8F5D3E11C1E663D3A1DCB03859570B17B1017C678F09F1F984A1CC6BB13BAAA0082B3DC9EC6AC1E37CD34F8202C4870792F8323AC9FF76E4CB8C56461166CB65A3B60CCDD0C5C6BEE504B37FBEC5280583FCD851159213249881416FD5785000F8BB0AED6D442128D02518FE84D108E31A94A83022B4BB606EE26F209FD29E3BA2D07AAF8B8DCAF778391489C6C6AB01B985935CD4CDC2CD554D5692F86B6CD3BB20BB6B623937F4328B2AB5D811F7B20D6A135873B7ADC797E1315D23350F6D703D306C6CDF1E6876FD4E1BED37A4B7E2385B306C1582E4E5BB0BEB74C4F6119429090872CB2CF71A60D2F74D8A0D0B6CFEE6783920450B8E204EF10D73DA5FE8D5EEB79F7F3C9B86C8A9FFB6B8B15FF0EFEBDE3D394339F536DF589AD94A0D66476A9C8F242D6768B767593B96050E2DF7A1857223B42F2DE5F6521187DEA7D25B8E9F8C4EB2C0B7B8D3FB77C387062E95804F5F242D6768B767593B96050E2DF7A1857361D15759F67C985726DF0C64E10BC1966FD8C41DDEAAFE6F6195FB947830441F813567819C6C39A294BCBF081D5D38FC18AD1AE35C67FC539177E4BCF2B5B8DEFB52A39AFDEA807E2F356A8C805EC18EA0F32F53D1A58E198D1107945D6E13C125EE9743FF5CA07A14045B54FE5EC2BDF55A1E03E487017E3E3DEBC93D209660DA01F1FFE9831003FB2035733AF7904FFF1827FB3E4E1B418F8A39B207A0B312118BE4E3CEE45F82E0DCCDB92C4169E6D1884741DA5714915762B30BDFC367600C5CABCFCB77CC8A6EC672F5B9DE8B15BE28F3D180CE37A2559ED48146C0FF84EDF61B91027937ECA1539726385B343C1866A4C8801BF0EA41476756BDA062EDEA6AF497FE11EB75B10F47DEF5351848E7A4B677B83B3B6C7952998487F1135F242D6768B767593B96050E2DF7A1857868549407206CA374D5C54C21399A2AB07F09778BA7CFF2A5347E059E01E7ABC3E6A7D9651964E711831579002EDF2519BFCF7AFB2B92245639A5317E5BEADAA9EA6FEF89D478BA06F152B30C9A3F63175CD89A8EFFB0FF8D68C6E9E2CE14294166EFF7B9316331F3F3843B3F97321570E318ED3FC9432C010A1CA5D07F2B9A91E571BC4957AAC3D1572FEDC48EE53549437ABF39EFEFC6229D5E7ECF0644C6E51B4B31519624F2C5EDC125F6E4055A7C699CAB5BC93BA5F7B8C7FDFB925A1B879C7AE2838D4C8074B2BD54D2C5D5AE960A50F8B34DF2F5DE64F79765BC3385A";
		System.out.println(decrypt(secret,key));*/


		//System.out.println( UUID.randomUUID().toString().replace("-",""));
		for (int i=0;i<100;i++){
			KeyGenerator kg = KeyGenerator.getInstance("AES");
			kg.init(256);//要生成多少位，只需要修改这里即可128, 192或256
			SecretKey sk = kg.generateKey();
			//System.out.println(sk);
			byte[] b = sk.getEncoded();
			System.out.println(b.length);
		}

	}
}
